<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // 实现一个电子钟：都是汉字中文 显示年月日 时分秒
        // 需要把数字转成汉字
        // 传入数字进来 我返还汉字
        function formatNum(num) {
            var arr = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九', '十'];
            if (0 <= num && num <= 10) {
                return arr[num];
            } else if (num < 20) {
                // console.log((num+"")[1]);
                var snum = arr[(num + "")[1]];
                // console.log(snum);
                return "十" + snum;
            } else if (num > 20 && num % 10 !== 0) {
                var snum1 = arr[(num + "")[1]];  // 个位
                var snum2 = arr[(num + "")[0]];  // 十位
                // console.log(snum1,snum2);
                return snum2 + "十" + snum1;
            } else if (num >= 20 && num % 10 == 0) {
                var snum2 = arr[(num + "")[0]];
                return snum2 + "十";
            }
        }


        //    console.log( formatNum(50));

        // 处理年份的函数：传入数字的年份返还汉字的年份  ,依赖了formatNum函数；
        function formartYear(year) {
            year = year + "";
            var str = "";
            for (var i = 0; i < year.length; i++) {
                str += formatNum(year[i]);
            }
            // console.log(str);
            return str + "年";
        }
        console.log(formartYear(2020));
        //    生成最新的时间
        function getTimeFn() {
            var time = new Date();
            var year = time.getFullYear();
            var mounth = time.getMonth() + 1;
            var date = time.getDate();
            var hour = time.getHours();
            var minute = time.getMinutes();
            var second = time.getSeconds();
            var timeStr = formartYear(year) + " " + formatNum(mounth) + "月 " + formatNum(date) + "日 " + formatNum(hour) + "时 " + formatNum(minute) + "分 " + formatNum(second) + "秒";
            return timeStr;
        }

        // console.log(timeStr)
        setInterval(() => {
            document.body.innerHTML = getTimeFn();
        }, 10);


    </script>
</body>

</html>